//
//  LoginViewController.m
//  ZFPlatform
//
//  Created by magus on 11-5-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"
#import "CTextField.h"
#import "RegisterViewController.h"
#import "FindPasswordViewController.h"
#import "CardSettingViewController.h"


@implementation LoginViewController

@synthesize	userName;
@synthesize password;
@synthesize loginBtn;
@synthesize registerBtn;
@synthesize savePwdBtn;
@synthesize findPwdBtn;
@synthesize bgPanel;


//通过xib资源初始化视图控制器
//
+ (LoginViewController*) InitFromXib
{
	self = [[LoginViewController alloc] 
			initWithNibName:@"LoginViewController" bundle:nil];
	return self;
}


//初始化界面
//
- (void) InitAllViews
{
	//设置导航栏标题
	[self setTitle:@"登录"];

	//设置导航栏左按钮
	UIImage* image = [UIImage imageNamed: @"导航栏按钮（返回）.png"];
	CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
	UIButton* leftButton = [[UIButton alloc] initWithFrame:rect];
	[leftButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
	[leftButton setTitle:@" 返回" forState:UIControlStateNormal];
	[leftButton setBackgroundImage:image forState:UIControlStateNormal];
	[leftButton addTarget:self action:@selector(GoBack:) forControlEvents:UIControlEventTouchDown];
	UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
	self.navigationItem.leftBarButtonItem = leftItem;
	[leftButton release];
	[image release];
	
	//设置背景面板成圆角显示
	[bgPanel.layer setCornerRadius:12.0];
	
	//设置“记住密码”图片
	UIImage* savePwdImg = [UIImage imageNamed:@"未选中.png"];
	[savePwdBtn setBackgroundImage:savePwdImg forState:UIControlStateNormal];
	[savePwdImg release];
	
	//设置“注册”按钮背景
	UIImage* registerImg = [UIImage imageNamed:@"按钮（小）灰色.png"];
	[registerBtn setBackgroundImage:registerImg forState:UIControlStateNormal];
	[registerImg release];
	
	//设置“登录”按钮背景
	UIImage* loginImg = [UIImage imageNamed:@"按钮（小）.png"];
	[loginBtn setBackgroundImage:loginImg forState:UIControlStateNormal];
	[loginImg release];
	
	//如果已保存密码，在此读取
	// ......
}


//响应导航栏返回按钮
//
- (void) GoBack: (id) sender
{
	[self.navigationController popViewControllerAnimated:YES];
}


//使按return键后退出软键盘
//
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


//响应文本框开始编辑
//
- (IBAction) OnEditBegin: (id) sender
{
	CTextField* textField = (CTextField*)sender;
	
	[textField SetBorderWidth:4.0];
	[textField SetCornerRadius:8.0];
	[textField SetBorderColor:[UIColor orangeColor]];
	[textField setTextColor:[UIColor blackColor]];
}


//响应文本框编辑完毕
//
- (IBAction) OnEditEnd: (id) sender
{
	CTextField* textField = (CTextField*)sender;
	
	[textField SetBorderWidth:0.0];
	[textField setTextColor:[UIColor lightGrayColor]];
}


//响应"保存密码"按钮
//
- (IBAction) OnClick_SavaPwd: (id) sender
{	
	if (!bSavePwd)
	{
		//设置“记住密码”图片
		UIImage* savePwdImg = [UIImage imageNamed:@"选中.png"];
		[savePwdBtn setBackgroundImage:savePwdImg forState:UIControlStateNormal];
		[savePwdImg release];
		bSavePwd = YES;
		
		//在此处删除本地已保存密码
		// ......
	}
	else
	{
		//设置“记住密码”图片
		UIImage* savePwdImg = [UIImage imageNamed:@"未选中.png"];
		[savePwdBtn setBackgroundImage:savePwdImg forState:UIControlStateNormal];
		[savePwdImg release];
		bSavePwd = NO;
		
		//在此处保存密码到本地
		// ......
	}
}


//响应"找回密码"按钮
//
- (IBAction) OnClick_FindPwd: (id) sender
{
	[findPwdBtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
	
	if(findPasswordViewController == nil)
		findPasswordViewController = [FindPasswordViewController InitFromXib];
	
	[self.navigationController pushViewController:findPasswordViewController animated:YES];
}


//响应"登录"按钮
//
- (IBAction) OnClick_Login: (id) sender
{
	//NSString* strUserName = [userName text];
	//NSString* strPassword = [password text];
	
	
	//在此验证登录信息
	// ......
	
	
	//切换到“信用卡设置”
	if(cardSettingViewController == nil)
		cardSettingViewController = [CardSettingViewController InitFromXib];
	
	[self.navigationController pushViewController:cardSettingViewController animated:YES];
}


//响应"注册"按钮
//
- (IBAction) OnClick_Register: (id) sender
{
	if (registerViewController == nil)
		registerViewController = [RegisterViewController InitFromXib];
	
	[self.navigationController pushViewController:registerViewController animated:YES];
}


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//初始化界面
	[self InitAllViews];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}


- (void)dealloc
{
	[findPasswordViewController release];
	[registerViewController release];
	[bgPanel release];
	[findPwdBtn release];
	[savePwdBtn release];
	[registerBtn release];
	[loginBtn release];
	[password release];
	[userName release];
	[registerViewController release];
	
    [super dealloc];
}


@end
